package com.jfinal.weixin.demo.xingzuo;

import com.alibaba.fastjson.JSON;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * http://avatardata.cn/Docs/Api/73301156-98ce-49bd-8741-71a6c49ef0e6
 * Created by wangxiaoyang on 16/8/28.
 */
public class XingzuoApi {

    private static final String baseUrl = "http://api.avatardata.cn/Constellation/Query?key=8f59b7f33a1b4c3fb35594900ea04aac&consName=";
    private static final String type_today = "&type=today";

    public static void main(String[] args) {
        getXingzuo(new OkHttpClient(), "金牛");
    }

    public static XingzuoBean getXingzuo(OkHttpClient httpClient , String xingzuo) {
        if( null != xingzuo && xingzuo.length() == 2 && !xingzuo.contains("座") ) {
            xingzuo += "座";
        }
        String url = baseUrl + xingzuo + type_today;
        Request request = new Request.Builder()
                .url(url)
                .build();
        try {
            Response response = httpClient.newCall(request).execute();
            String result = response.body().string();
            System.out.println(result);
            XingzuoBean xingzuoBean = JSON.parseObject(result,XingzuoBean.class);
            System.out.println(xingzuoBean.format());
            return xingzuoBean;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
